<?php

/**
 * 高德地图地址【省市区】解析
 * @author chenshubo
 */
namespace App\Service\VendorClient\GaoDeAddressApi;
use App\Service\BaseService;
use App\Service\AppService\IdService;
use App\Service\TaskServer\TaskWorker;
use App\Service\AppService\SystemCacheConfigService;

class GaoDeAddressApiClient extends BaseService
{
    protected $config;
    public function __construct()
    {
        $this->config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('gdmap');
    }
    
    /**
     * 查询区域清单明细
     * @param array $params
     * @return mixed
     */
    public function queryRegionList( $param = [] )
    {
        $parameters = [
            'method' => 'queryRegionList',
            'data'   => $param
        ];
        return $this->execute($parameters);
    }
    
    /**
     * 解析当前物理地址
     * @param array $params
     * @return mixed
     */
    public function queryRegionByAddress( $param = [] )
    {
        $data = [
            'address' => $param['address'],
            'output'  => 'json'
        ];
        $parameters = [
            'method' => 'queryRegion',
            'data'   => $data
        ];
        return $this->execute($parameters);
    }
    
    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function execute( $param = [] )
    {
        $method     = $param['method'];
        $url        = $this->config['queryRegion'];
        $data       = $param['data'];
        if( !empty($param['data']) ) {
            $url = $url."&".http_build_query($param['data']);
        }
        $parameter = [
            [
                'tag'       => 'gd_address',
                'uid'       => IdService::instance()->getLogID(),
                'url'       => $url,
                'data'      => $data,
                'method'    => 'get',
                'format'    => 'json',
                'header'    => [],
            ],
        ];
        $resTask = TaskWorker::instance()->addTask($parameter);
        $resTask = current($resTask);
        $result  = @json_decode($resTask['response']['result'],true);
        return $result;
    }
}
